package com.zslibrary.StateHandle;

import android.os.SystemClock;
import android.util.Log;

import com.zslibrary.BusMaster.HandleError;
import com.zslibrary.Enum.StateDefine;
import com.zslibrary.Enum.StateEventDefine;
import com.zslibrary.Pipe.Pipe;
import com.zslibrary.Protocol.Poll;
import com.zslibrary.Structure.ErrorStruct;
import com.zslibrary.Tools.ZSLog;

public class ErrorState extends StateHandle {
    private Pipe mPipe;
    private ErrorStruct mErrorCode;

    public ErrorState(Pipe pipe,ErrorStruct error){
        mPipe=pipe;
        mErrorCode=error;
    }

    @Override
    public @StateEventDefine.StateEvent int handle(){
        ZSLog.d("error state");
        int state=Poll.masterPoll(mPipe);
        if(state==StateEventDefine.EVT_DISCONNECT) {
            HandleError.setError(mErrorCode);
            return StateEventDefine.EVT_DISCONNECT;
        }
        HandleError.clearError(mErrorCode.errorCode);
        return state;
    }

    @Override
    public @StateDefine.State int getNextHop(@StateEventDefine.StateEvent int event) {
        if(event== StateEventDefine.EVT_NONE)
            return StateDefine.STATE_RECONNECT;
        else if(event==StateEventDefine.EVT_RESET)
            return StateDefine.STATE_INIT;
//            else if(event==StateEventDefine.EVT_EXECUTE)
//                return StateDefine.STATE_BUSY;
        return StateDefine.STATE_ERROR;
    }
}
